parse.test.js ➔ ... ➔ describe   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 171

Duplication

Lines 56
Ratio 32.75 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
c 3
b 1
f 1
nc 1
nop 0
dl 56
loc 171
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import chai from 'chai';
2
import chaiaspromised from 'chai-as-promised';
3
4
import cgparse from '../../src/codingame/parse.js';
5
import CodingameError from '../../src/codingame/error.js';
6
7
let expect = chai.expect;
8
chai.use(chaiaspromised);
9
10
describe(`[module] codingame/parse`, function() {
11
	describe(`[method] parse`, function() {
12
		it(`should succeed with a successful response and a successful test`, function() {
13
			let output = `42`;
14
			let response = {
15
				success: {
16
					"output": output,
17
					"comparison": {
18
						"success": true
19
					}
20
				}
21
			};
22
			let parse = cgparse.parse(response);
23
			return expect(parse).to.be.fulfilled
24
				.and.to.eventually.be.equal(output);
25
		});
26
		it(`should reject if success-like response is malformed`, function() {
27
			let output = `42`;
28
			let response = {
29
				success: {
30
					"output": output,
31
					"comparison": {
32
						"success": false
33
					}
34
				}
35
			};
36
			let parse = cgparse.parse(response);
37
			return expect(parse).to.be.rejected
38
				.and.to.eventually.be.an.instanceof(Error);
39
		});
40
		it(`should reject when found value is not the expected one`, function() {
41
			let expected = `42`;
42
			let found = `43`;
43
			let response = {
44
				"success": {
45
					"output": found,
46
					"comparison": {
47
						"success": false,
48
						"found": found,
49
						"expected": expected
50
					}
51
				}
52
			};
53
			let parse = cgparse.parse(response);
54
			return expect(parse).to.be.rejected
55
				.and.to.eventually.be.an.instanceof(CodingameError)
56
				.and.to.have.property(`message`)
57
					.with.string(expected)
58
					.and.with.string(found);
59
		});
60
		it(`should reject if expected-like response is malformed`, function() {
61
			let found = `43`;
62
			let response = {
63
				"success": {
64
					"output": found,
65
					"comparison": {
66
						"success": false,
67
						"found": found
68
					}
69
				}
70
			};
71
			let parse = cgparse.parse(response);
72
			return expect(parse).to.be.rejected
73
				.and.to.eventually.be.an.instanceof(Error);
74
		});
75 View Code Duplication
		it(`should resolve with last information when frames are arriving`, function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
76
			let infos = `Landing phase starting\nX=2500m, Y=2700m, HSpeed=0m/s VSpeed=0m/s\nFuel=550l, Angle=0°, Power=0 (0.0m/s2)\n`;
77
			let stdout = `0 0`;
78
			let stderr = `debug information`;
79
			let view = ` 0\n7000 3000 3.711 1.0 1.0 1 0 4 -90 90\n20 40 10 20 DIRECT 15 1\n7\n0 100\n1000 500\n1500 1500\n3000 1000\n4000 150\n5500 150\n6999 800\n2500 2700 0 0 550 0 0\n`;
80
			let response = {
81
				"success": {
82
					"frames": [{
83
						"gameInformation": infos,
84
						"stdout": stdout,
85
						"stderr": stderr,
86
						"view": view,
87
						"keyframe": true
88
					}],
89
					"gameId": 154447808,
90
					"scores": [ 1 ],
91
					"metadata": {
92
						"fuel": 0
93
					}
94
				}
95
			};
96
			let parse = cgparse.parse(response);
97
			return expect(parse).to.be.fulfilled
98
				.and.to.eventually.be.with.a.string(infos.trim())
99
					.and.with.a.string(stdout)
100
					.and.with.a.string(stderr);
101
		})
102 View Code Duplication
		it(`should reject when frames are arriving but score is 0`, function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
103
			let subinfo = `Success: Mars Lander landed hard but Opportunity is ok!`;
104
			let infos = `¤GREEN¤${subinfo}§GREEN§\nX=2500m, Y=1167m, HSpeed=0m/s VSpeed=-47m/s\nFuel=456l, Angle=0°, Power=4 (4.0m/s2)\n`;
105
			let stdout = `0 0`;
106
			let stderr = `debug information`;
107
			let view = ` 0\n7000 3000 3.711 1.0 1.0 1 0 4 -90 90\n20 40 10 20 DIRECT 15 1\n7\n0 100\n1000 500\n1500 1500\n3000 1000\n4000 150\n5500 150\n6999 800\n2500 2700 0 0 550 0 0\n`;
108
			let response = {
109
				"success": {
110
					"frames": [{
111
						"gameInformation": infos,
112
						"stdout": stdout,
113
						"stderr": stderr,
114
						"view": view,
115
						"keyframe": true
116
					}],
117
					"gameId": 154447808,
118
					"scores": [ 0 ],
119
					"metadata": {
120
						"fuel": 0
121
					}
122
				}
123
			};
124
			let parse = cgparse.parse(response);
125
			return expect(parse).to.be.rejected
126
				.and.to.eventually.have.a.property(`message`)
127
					.with.string(subinfo)
128
					.and.with.a.string(stdout)
129
					.and.with.a.string(stderr);
130
		})
131
		it(`should reject when bundle cannot compile`, function() {
132
			let message = `SyntaxError: EOL while scanning string literal`;
133
			let stacktrace = [{
134
				"location": `ANSWER`,
135
				"container": `Answer.py`,
136
				"function": ` not in a function`,
137
				"line": 26
138
			}];
139
			let response = {
140
				"success": {
141
					"error": {
142
						"message": message,
143
						"stacktrace": stacktrace
144
					}
145
				}
146
			};
147
			let parse = cgparse.parse(response);
148
			return expect(parse).to.be.rejected
149
				.and.to.eventually.be.an.instanceof(CodingameError)
150
				.and.to.have.property(`message`)
151
				.with.string(message);
152
		});
153
		it(`should reject with an error when compile-like response is malformed`, function() {
154
			let message = `SyntaxError: EOL while scanning string literal`;
155
			let stacktrace = {
156
				"location": `ANSWER`,
157
				"container": `Answer.py`,
158
				"function": ` not in a function`,
159
				"line": 26
160
			};
161
			let response = {
162
				"success": {
163
					"error": {
164
						"message": message,
165
						"stacktrace": stacktrace
166
					}
167
				}
168
			};
169
			let parse = cgparse.parse(response);
170
			return expect(parse).to.be.rejected
171
				.and.to.eventually.be.an.instanceof(Error)
172
				.and.to.have.property(`message`);
173
		});
174
		it(`should reject with an error when fail to parse the response`, function() {
175
			let response = { "fakeproperty": false };
176
			let parse = cgparse.parse(response);
177
			return expect(parse).to.be.rejected
178
				.and.to.eventually.be.an.instanceof(Error)
179
				.and.to.have.property(`message`);
180
		});
181
	});
182
});
183